<template>

    <!-- 农业技术信息 -->

    <div class="CcadNyjsSyj">
        <!-- 选择器 -->
        <el-row class="selectInfo">
            <!-- 类别选择器 -->
            <el-col :span="7">
                <h5 class="textStyle">类别</h5>
                <el-select v-model="selectCategory" placeholder="请选择类别" clearable @change="startSearch">
                    <el-option v-for="(item, index) in allCategory" :key="index" :label="item.label"
                        :value="item.value">
                    </el-option>
                </el-select>
            </el-col>
            <!-- 搜索数据输入框 -->
            <el-col :span="9">
                <h5 class="textStyle">搜索数据</h5>
                <el-input placeholder="请输入内容" v-model="searchInput" clearable class="inputStyle"  @blur="startSearch" @input="startSearch">
                </el-input>
            </el-col>
        </el-row>
        <!-- 表格 -->
        <el-table :data="tableData" style="width: 100%" stripe class="center-table" height="745">
            <el-table-column label="序号" prop="num" sortable width="80" align="center">
            </el-table-column>
            <el-table-column label="类别" prop="category" width="160" align="center">
            </el-table-column>
            <el-table-column label="技术名称" prop="jsname" sortable width="290" align="center">
            </el-table-column>
            <el-table-column label="发布日期" prop="publishdate" sortable width="220" align="center">
            </el-table-column>
            <el-table-column label="发布单位" prop="pubinstitution" width="220" align="center">
            </el-table-column>
            <el-table-column label="技术详情" prop="detail" width="450" header-align="center" show-overflow-tooltip>
            </el-table-column>
            <el-table-column label="数据入库时间" prop="inserttime" sortable width="200" align="center">
            </el-table-column>
        </el-table>

        <!-- 分页器 -->
        <div class="block">
            <el-pagination @current-change="handleCurrentChange" :current-page.sync="currentPage" :page-size="PageSize"
                layout="total, prev, pager, next , jumper" :total="tableLength" :pager-count="7" background
                :hide-on-single-page="false">
            </el-pagination>
        </div>
    </div>
</template>

<script>
import { getDataInterface, getCategory, getSearch } from '../../global'
export default {
    name: 'CcadNyjsSyj',
    data() {
        return {
            allDataSubUrl: '/CcadNyjsSyj/findByPage',
            CategoryUrl: '/CcadNyjsSyj/category',
            searchUrl: '/CcadNyjsSyj/findByPagewithParam',
            // 从后端获取表格中数据
            tableData: [],
            // 所有数据的长度,还未从后端获取
            tableLength: -1,
            // 查找的关键字
            search: '',
            // 当前所在的页码
            currentPage: 1,
            // 每页显示的数据条数
            PageSize: 10,
            token: '',
            allCategory: [],
            selectCategory: '',
            selectData: '',
            searchInput: ''
        }
    },
    methods: {
        handleCurrentChange(val) {
            console.log(`当前页: ${val}`);
            // 改变默认的页数
            this.currentPage = val
            const dataPromise = getDataInterface(this.allDataSubUrl, this.currentPage, this.PageSize)
            dataPromise.then(res => {
                this.tableData = res.data
                this.tableLength = res.total
            })
        },
        startSearch() {
            console.log("--------开始搜索------------")
            if (this.searchInput.length !== 0 || this.selectCategory.length !== 0) {
                const dataPromise = getSearch(this.searchUrl, this.currentPage, this.PageSize,this.selectCategory, this.searchInput)
                dataPromise.then(res => {
                    this.tableData = res.data
                    this.tableLength = res.total
                })
            }else {
                this.getTableData()
            }
        },
        getTableData() {
            const dataPromise = getDataInterface(this.allDataSubUrl, this.currentPage, this.PageSize)
            // data就是传入resolve的response.data
            dataPromise.then(res => {
                this.tableData = res.data
                this.tableLength = res.total
            })
        }
    },
    mounted() {
        // 获取所有类别数据
        const categoryPromise = getCategory(this.CategoryUrl)
        categoryPromise.then(resp => {
            // console.log("--------------------")
            // console.log(resp)
            this.allCategory = resp.map(item => {
                return { value: item, label: item }
            })
            console.log(this.allCategory)
        })

        // 获取所有数据
        this.getTableData()
    }
}
</script>

<style scoped lang="less">
.CcadNyjsSyj {
    .el-col {
        display: flex;
    }

    .selectInfo {
        padding-bottom: 20px;
    }

    .inputStyle {
        width: 350px;
    }

    .textStyle {
        line-height: 40px;
        padding-right: 20px;
    }

    .block {
        margin-top: 15px;
        text-align: center;
    }
}
</style>
<style>
.el-tooltip__popper {
    max-width: 800px;
}
</style>